package com.carsystems.controller;

import com.carsystems.mapper.ViolationTypeMapper;
import com.carsystems.pojo.ViolationType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ViolationTypeController {
    @Autowired
    private ViolationTypeMapper violationTypeMapper;

    //查询全部违规类型
    @GetMapping("/ViolationList")
    public List<ViolationType> ViolationList() {
        List<ViolationType> violationTypes = violationTypeMapper.ViolationList();
        return violationTypes;
    }

    //按type_id查询单条违规类型
    @GetMapping("/typeselectByPrimaryKey")
    public ViolationType selectByPrimaryKey(int type_id) {
        return violationTypeMapper.selectByPrimaryKey(type_id);
    }

    //增加一条违规类型
    @ResponseBody
    @PostMapping("/addViolationType")
    public String addViolationType(@RequestBody ViolationType violationType) {
        violationTypeMapper.addViolationType(violationType);
        if (violationType != null) {
            return "success";
        } else {
            return "fail";
        }
    }

    //修改一条违规类型
    @ResponseBody
    @PutMapping("/updateViolationType")
    public String updateViolationType(@RequestBody ViolationType violationType){
        int row = violationTypeMapper.updateViolationType(violationType);
        violationTypeMapper.selectByPrimaryKey(violationType.getType_id());
        if (row == 1) {
            return "success";
        } else {
            return "fail";
        }
    }

    //删除一条违规类型
    @ResponseBody
    @DeleteMapping("/deleViolationType")
    public  String deleViolationType( ViolationType violationType){
        int row = violationTypeMapper.deleViolationType(violationType.getType_id());
        if (row == 1) {
            return "success";
        } else {
            return "fail";
        }
    }
}